<script>
	import {
		wxAuthorization
	} from './utils/api/api.js'
	export default {
		onLaunch: function() {
			
		},
		onShow: function() {
			this.getWecharInfo()
		},
		onHide: function() {
			
		},
		methods: {
			getWecharInfo() {
				uni.login({
					provider: 'weixin',
					success(r) {
					    console.log(r.code);
					    // 调用后端接口获取用户信息，传入code  
						wxAuthorization({
							code: r.code
						}).then(res => {
							console.log(res)
							if (res.code == "404") {
								uni.setStorageSync('BindInfo', {
									openId: res.msg
								});
								uni.navigateTo({
									url: '/pages/login/login'
								})
							} else {
								let timestamp = new Date().getTime() - 10000; //当前时间戳-10秒
								uni.setStorageSync('publictoken', res.data.access_token)
								uni.setStorageSync('publicrefresh_token', res.data.refresh_token)
								uni.setStorageSync('expires_in', timestamp + res.data.expires_in)
								uni.setStorageSync('BindInfo', res.data.weChatUserInfo);
								uni.switchTab({
									url: "/pages/index/index?token='has'"
								})
							}
						});
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import '@/uni_modules/uni-scss/index.scss';
	/* #ifndef APP-NVUE */
	@import '@/static/customicons.css';
	// 设置整个项目的背景色
	page {
		background-color: #f5f5f5;
	}

	/* #endif */
	.example-info {
		font-size: 14px;
		color: #333;
		padding: 10px;
	}
</style>
